package com.ncmmall.api.apis.mall;


import com.ncmmall.api.apis.service.Result;
import com.ncmmall.api.params.WebUserDTO;
import com.ncmmall.api.security.Securitys;
import com.ncmmall.application.msm.MsmService;
import com.ncmmall.cache.redis.RedisService;
import com.feijin.commons.lang.Randoms;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.security.user.WebUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

/**
 * @author linxs
 *         短信验证码
 */

@RestController
@RequestMapping(value = "/mobile")
@Component
public class MobileApi {

    private Logger log = LoggerFactory.getLogger(MobileApi.class);

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisService redisService;


    /**
     * 获取短信验证码
     *
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/code", method = RequestMethod.GET)
    @ResponseBody
    public Result code(@RequestParam("mobile") String mobile) {
        Result result = new Result();
        try {
            Requires.hasLength(mobile, "参数不正确");

            String mobileCode = Randoms.getRandomNumr(6);
            log.info(mobile + "  [验证码]  " + mobileCode);

            msmService.send(mobile, URLEncoder.encode("【拖拖部落】验证码: " + mobileCode + "，有效期5分钟。", "UTF-8"));
            redisService.set("msm-" + mobile, mobileCode, 5L);

        } catch (Exception e) {
            log.error("[获取手机验证码异常：]", e);
            result.fail().setMsg("验证码发送失败");
        }

        return result.ok();
    }

    /**
     * 设置支付宝密码前校验短信
     *
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/sendPayPasswordCode", method = RequestMethod.GET)
    @ResponseBody
    public Result sendPayPasswordCode(HttpServletRequest request) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            String mobileCode = Randoms.getRandomNumr(4);
            log.info("验证码：" + mobileCode);

            msmService.send(member.getMobile(), URLEncoder.encode("【拖拖部落】验证码: " + mobileCode + "，有效期5分钟。", "UTF-8"));
            redisService.set("msm-" + member.getMobile(), mobileCode, 5L);

        } catch (Exception e) {
            log.error("[获取手机验证码异常：]", e);
            result.fail().setMsg("验证码发送失败");
        }

        return result.ok();
    }

    /**
     * 校验验证码是否正确
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/checkPayPasswordCode", method = RequestMethod.POST)
    @ResponseBody
    public Result payPasswordMessage(HttpServletRequest request, @RequestBody WebUserDTO params) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);
            String code = redisService.get("msm-" + member.getMobile());
            if (!params.getMobileCode().equals(code)) {
                return result.fail().setMsg("验证码不正确");
            }

            return result.ok();

        } catch (Exception e) {
            log.error("[校验支付短信验证码异常]", e);
            result.fail().setMsg("网络异常");
        }

        return result.ok();
    }


}


